package Testdmo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Testdmo1.Date: 2024-02-27
 * Time：22:08
 */
public class J2_27 {
    //    方法一：排序找中位数即为多数元素
    public static int func3(int []arr8){
        Arrays.sort(arr8);
        return arr8[arr8.length/2];
    }

    //    方法二：正反票原则
    public static int func4(int []arr9) {
        int count=1;
        int tmp=arr9[0];
        int ret=0;
        for (int i = 1; i < arr9.length; i++) {

            if (tmp == arr9[i]) {
                count++;
            } else {
                ret=arr9[i];
                count--;
            }
        }

        if (count>0) {
            return tmp;
        }

        return ret;
    }
    public static void main8(String[] args) {
        int []arry={2,2,1,1,1,2,2,1,1,2,2,2};
//        int ret=func3(arry);
        int ret =func4(arry);
        System.out.println(ret);
    }

    // 三种打印
    public static void main9(String[] args) {
        int []array={1,2,3,4,5,6,7,8,9,10};
        System.out.println("循环打印");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println("\n赋值打印");
        for (int x:array) {
            System.out.print(x+" ");
        }
        System.out.println("\n字符串转化打印");
        String str=Arrays.toString(array);
        System.out.println(str);
    }

//    二分查找 专用类方法 binarySearch

    public static void main10(String[] args) {
        int []array={1,2,3,4,5,6,7,8,9,10};

        int ret=Arrays.binarySearch(array,5);
        System.out.println("该数组下标为： "+ret);

    }

    //    数组排序
    public static void main11(String[]args) {
        double []array1={7.3,1.24,4.19,9.37,3.09,5.44,2.11,0.13,10.25};

//        部分排序
//        排序元素下标 [5,9)
//        Arrays.sort(array,5,9);

        System.out.println(Arrays.toString(array1));

//        整体排序
        Arrays.sort(array1);
        System.out.println(Arrays.toString(array1));


    }


    public static void main12(String[] args) {
        int []array=new int[10];

        // 部分填充
        //        注意参数顺序
        Arrays.fill(array,2,8,4);
        System.out.println(Arrays.toString(array));

        //    整体填充
        Arrays.fill(array,3);
        System.out.println(Arrays.toString(array));
    }

    //    一个对象对应多个应用
    public static void main13(String[] args) {
        int []array1=new int[]{1,2};
        int []array2=new int[4];
        array2[0]=100;
        array2[1]=200;

        array1=array2;
        array1[2]=300;
        array1[3]=400;

//        两个引用同时指向一个对象

        System.out.println(Arrays.toString(array1));

//        没有引用指向的那个对象则被销毁
        System.out.println(Arrays.toString(array2));

        //置为 null 时也能为重新 new 一个对象
        int []array3=null;

        array3=new int[4];
        System.out.println(Arrays.toString(array3));
    }

    //    两数组对比
    public static void main14(String[] args) {

        double []array1={7.3,1.24,4.19,9.37,3.09,5.44,2.11,0.13,10.25};
        double []array2={7.3,1.24,4.19,9.37,3.09,5.44,2.11,0.13,10.25};
        double []array3= {6.61};

        // 判断是否相同
        boolean b=Arrays.equals(array1,array2);
        System.out.println(b);

//        对比哪个数组更大
        System.out.println("array1 与 array2 比较");
        int i=Arrays.compare(array1,array2);
        System.out.println(i);

//        逐个比较
        Arrays.sort(array1);
        System.out.println("array1 与 array3 比较");
        i=Arrays.compare(array1,array3);
        System.out.println(i);
    }


    //    数组拉长
    public static void main(String[] args) {
        String []array1={"abc","def","hil"};
        String[]array2=new  String[4];

//        需要足够长度
        array2=Arrays.copyOf(array1,3);
        System.out.println(Arrays.toString(array2));

        array2=Arrays.copyOfRange(array1,1,3);
        System.out.println(Arrays.toString(array2));

    }
}
